package com.asdc.autoscanfiles.repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.asdc.autoscanfiles.entity.ImportLog;

@Repository("importLogRepository")
public class ImportLogRepository {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int[] batchAdd(List<ImportLog> logs) {
		if (!CollectionUtils.isEmpty(logs)) {
			String sql = "insert into qxj.import_log(file_name, file_path, update_time) values(?,?,?)";
			List<Object[]> params = new ArrayList<>();
			for (ImportLog log : logs) {
				Object[] param = { log.getFileName(), log.getFilePath(), log.getUpdateTime() };
				params.add(param);
			}
			this.jdbcTemplate.batchUpdate(sql, params);
		}
		return null;
	}

	public List<ImportLog> getLatestLogs(int size) {
		String sql = "select * from qxj.import_log order by id desc limit 0," + size;
		return this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<ImportLog>(ImportLog.class));
	}
	
	public int getLogCount() {
		int count = this.jdbcTemplate.queryForObject("select count(id) from qxj.import_log", Integer.class);
		return count;
	}
}
